//package com.scwatch.core.web.fore;
//
//import com.octo.captcha.service.CaptchaService;
//import com.scwatch.common.captcha.Captchas;
//import com.scwatch.common.web.Servlets;
//import com.scwatch.common.web.Validations;
//import com.scwatch.core.constant.Constants;
//import com.scwatch.core.domain.GlobalMail;
//import com.scwatch.core.domain.GlobalRegister;
//import com.scwatch.core.domain.Site;
//import com.scwatch.core.domain.User;
//import com.scwatch.core.service.MemberGroupService;
//import com.scwatch.core.service.OrgService;
//import com.scwatch.core.service.UserService;
//import com.scwatch.core.support.Context;
//import com.scwatch.core.support.ForeContext;
//import com.scwatch.core.support.Response;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//
///**
// * RegisterController
// *
// * @author gjl
// *
// */
//@Controller
//public class RegisterController {
//	/**
//	 * 注册模板
//	 */
//	public static final String REGISTER_TEMPLATE = "sys_member_register.html";
//	/**
//	 * 注册结果模板。提示会员注册成功，或提示会员接收验证邮件。
//	 */
//	public static final String REGISTER_MESSAGE_TEMPLATE = "sys_member_register_message.html";
//	/**
//	 * 验证会员模板
//	 */
//	public static final String VERIFY_MEMBER_TEMPLATE = "sys_member_verify_member.html";
//	/**
//	 * 忘记密码模板
//	 */
//	public static final String FORGOT_PASSWORD_TEMPLATE = "sys_member_forgot_password.html";
//	/**
//	 * 找回密码模板
//	 */
//	public static final String RETRIEVE_PASSWORD_TEMPLATE = "sys_member_retrieve_password.html";
//
//	@RequestMapping(value = { "/register",
//			Constants.SITE_PREFIX_PATH + "/register" })
//	public String registerForm(HttpServletRequest request,
//			HttpServletResponse response, org.springframework.ui.Model modelMap) {
//		Response resp = new Response(request, response, modelMap);
//		Site site = Context.getCurrentSite();
//		GlobalRegister registerConf = site.getGlobal().getRegister();
//		if (registerConf.getMode() == GlobalRegister.MODE_OFF) {
//			return resp.warning("register.off");
//		}
//		Map<String, Object> data = modelMap.asMap();
//		ForeContext.setData(data, request);
//		return site.getTemplate(REGISTER_TEMPLATE);
//	}
//
//	@RequestMapping(value = { "/register",
//			Constants.SITE_PREFIX_PATH + "/register" }, method = RequestMethod.POST)
//	public String registerSubmit(String captcha, String username,
//			String password, String email, String gender, Date birthDate,
//			String bio, String comeFrom, String qq, String msn, String weixin,
//			HttpServletRequest request, HttpServletResponse response,
//			org.springframework.ui.Model modelMap,String nickname) {
//		Response resp = new Response(request, response, modelMap);
//		Site site = Context.getCurrentSite();
//		GlobalRegister reg = site.getGlobal().getRegister();
//		String result = validateRegisterSubmit(request, resp, reg, captcha,
//				username, password, email, gender);
//		if (resp.hasErrors()) {
//			return result;
//		}
//
//		int verifyMode = reg.getVerifyMode();
//		String ip = Servlets.getRemoteAddr(request);
//		int groupId = reg.getGroupId();
//		int orgId = reg.getOrgId();
//		int status = verifyMode == GlobalRegister.VERIFY_MODE_NONE ? User.NORMAL
//				: User.UNACTIVATED;
//		User user = userService.register(ip, groupId, orgId, status, username,
//				password, email, null, null, null, gender, birthDate, bio,
//				comeFrom, qq, msn, weixin,nickname);
//		if (verifyMode == GlobalRegister.VERIFY_MODE_EMAIL) {
//			GlobalMail mail = site.getGlobal().getMail();
//			String subject = reg.getVerifyEmailSubject();
//			String text = reg.getVerifyEmailText();
//			userService.sendVerifyEmail(site, user, mail, subject, text);
//		}
//		resp.addData("verifyMode", verifyMode);
//		resp.addData("id", user.getId());
//		resp.addData("username", user.getUsername());
//		resp.addData("email", user.getEmail());
//		return resp.post();
//	}
//
//	@RequestMapping(value = { "/register_message",
//			Constants.SITE_PREFIX_PATH + "/register_message" })
//	public String registerMessage(String email, Integer verifyMode,
//			HttpServletRequest request, HttpServletResponse response,
//			org.springframework.ui.Model modelMap) {
//		Response resp = new Response(request, response, modelMap);
//		Site site = Context.getCurrentSite();
//		GlobalRegister reg = site.getGlobal().getRegister();
//		String username = Servlets.getParam(request, "username");
//		String result = validateRegisterMessage(request, resp, reg, username,
//				email, verifyMode);
//		if (resp.hasErrors()) {
//			return result;
//		}
//
//		User registerUser = userService.findByUsername(username);
//		modelMap.addAttribute("registerUser", registerUser);
//		modelMap.addAttribute("verifyMode", verifyMode);
//		Map<String, Object> data = modelMap.asMap();
//		ForeContext.setData(data, request);
//		return site.getTemplate(REGISTER_MESSAGE_TEMPLATE);
//	}
//
//	@RequestMapping(value = { "/verify_member",
//			Constants.SITE_PREFIX_PATH + "/verify_member" })
//	public String verifyMember(String key, HttpServletRequest request,
//			HttpServletResponse response, org.springframework.ui.Model modelMap) {
//		Response resp = new Response(request, response, modelMap);
//		List<String> messages = resp.getMessages();
//		Site site = Context.getCurrentSite();
//		if (!Validations.notEmpty(key, messages, "key")) {
//			return resp.badRequest();
//		}
//		User keyUser = userService.findByValidation(
//				Constants.VERIFY_MEMBER_TYPE, key);
//		userService.verifyMember(keyUser);
//		modelMap.addAttribute("keyUser", keyUser);
//		Map<String, Object> data = modelMap.asMap();
//		ForeContext.setData(data, request);
//		return site.getTemplate(VERIFY_MEMBER_TEMPLATE);
//	}
//
//	@RequestMapping(value = { "/forgot_password",
//			Constants.SITE_PREFIX_PATH + "/forgot_password" })
//	public String forgotPasswordForm(HttpServletRequest request,
//			HttpServletResponse response, org.springframework.ui.Model modelMap) {
//		Site site = Context.getCurrentSite();
//		Map<String, Object> data = modelMap.asMap();
//		ForeContext.setData(data, request);
//		return site.getTemplate(FORGOT_PASSWORD_TEMPLATE);
//	}
//
//	@RequestMapping(value = { "/forgot_password",
//			Constants.SITE_PREFIX_PATH + "/forgot_password" }, method = RequestMethod.POST)
//	public String forgotPasswordSubmit(String username, String email,
//			String captcha, HttpServletRequest request,
//			HttpServletResponse response, org.springframework.ui.Model modelMap) {
//		Response resp = new Response(request, response, modelMap);
//		String result = validateForgotPasswordSubmit(request, resp, username,
//				email, captcha);
//		if (resp.hasErrors()) {
//			return result;
//		}
//
//		Site site = Context.getCurrentSite();
//		User forgotUser = userService.findByUsername(username);
//		GlobalRegister reg = site.getGlobal().getRegister();
//		GlobalMail mail = site.getGlobal().getMail();
//		String subject = reg.getPasswordEmailSubject();
//		String text = reg.getPasswordEmailText();
//		userService.sendPasswordEmail(site, forgotUser, mail, subject, text);
//		resp.addData("username", username);
//		resp.addData("email", email);
//		return resp.post();
//	}
//
//	@RequestMapping(value = { "/retrieve_password",
//			Constants.SITE_PREFIX_PATH + "/retrieve_password" })
//	public String retrievePasswordForm(String key, HttpServletRequest request,
//			HttpServletResponse response, org.springframework.ui.Model modelMap) {
//		Response resp = new Response(request, response, modelMap);
//		List<String> messages = resp.getMessages();
//		if (!Validations.notEmpty(key, messages, "key")) {
//			return resp.badRequest();
//		}
//
//		Site site = Context.getCurrentSite();
//		User retrieveUser = userService.findByValidation(
//				Constants.RETRIEVE_PASSWORD_TYPE, key);
//		// 找不到用户、验证时间为空或者超过8小时，则验证失效。
//		if (retrieveUser == null
//				|| retrieveUser.getValidationDate() == null
//				|| System.currentTimeMillis()
//						- retrieveUser.getValidationDate().getTime() > 8 * 60 * 60 * 1000) {
//			return resp.post(501, "retrievePassword.invalidKey");
//		}
//		modelMap.addAttribute("retrieveUser", retrieveUser);
//		modelMap.addAttribute("key", key);
//		Map<String, Object> data = modelMap.asMap();
//		ForeContext.setData(data, request);
//		return site.getTemplate(RETRIEVE_PASSWORD_TEMPLATE);
//	}
//
//	@RequestMapping(value = { "/retrieve_password",
//			Constants.SITE_PREFIX_PATH + "/retrieve_password" }, method = RequestMethod.POST)
//	public String retrievePasswordSubmit(String key, String password,
//			HttpServletRequest request, HttpServletResponse response,
//			org.springframework.ui.Model modelMap) {
//		Response resp = new Response(request, response, modelMap);
//		List<String> messages = resp.getMessages();
//		if (!Validations.notEmpty(key, messages, "key")) {
//			return resp.post(401);
//		}
//		if (!Validations.notNull(password, messages, "password")) {
//			return resp.post(402);
//		}
//
//		User retrieveUser = userService.findByValidation(
//				Constants.RETRIEVE_PASSWORD_TYPE, key);
//		// 找不到用户、验证时间为空或者超过8小时，则验证失效。
//		if (retrieveUser == null
//				|| retrieveUser.getValidationDate() == null
//				|| System.currentTimeMillis()
//						- retrieveUser.getValidationDate().getTime() > 8 * 60 * 60 * 1000) {
//			return resp.post(501, "retrievePassword.invalidKey");
//		}
//		userService.passwordChange(retrieveUser, password);
//		return resp.post();
//	}
//
//	@ResponseBody
//	@RequestMapping(value = { "/check_username",
//			Constants.SITE_PREFIX_PATH + "/check_username" })
//	public String checkUsername(String username, String original,
//			HttpServletRequest request, HttpServletResponse response) {
//		Servlets.setNoCacheHeader(response);
//		if (StringUtils.isBlank(username)) {
//			return "true";
//		}
//		if (StringUtils.equals(username, original)) {
//			return "true";
//		}
//		// 检查数据库是否重名
//		boolean exist = userService.usernameExist(username);
//		if (!exist) {
//			return "true";
//		} else {
//			return "false";
//		}
//	}
//
//	private String validateRegisterSubmit(HttpServletRequest request,
//			Response resp, GlobalRegister reg, String captcha, String username,
//			String password, String email, String gender) {
//		List<String> messages = resp.getMessages();
//		if (!Captchas.isValid(captchaService, request, captcha)) {
//			return resp.post(100, "error.captcha");
//		}
//		if (reg.getMode() == GlobalRegister.MODE_OFF) {
//			return resp.post(501, "register.off");
//		}
//		Integer groupId = reg.getGroupId();
//		if (groupService.get(groupId) == null) {
//			return resp.post(502, "register.groupNotSet");
//		}
//		Integer orgId = reg.getOrgId();
//		if (orgService.get(orgId) == null) {
//			return resp.post(503, "register.orgNotSet");
//		}
//
//		if (!Validations.notEmpty(username, messages, "username")) {
//			return resp.post(401);
//		}
//		if (!Validations.length(username, reg.getMinLength(),
//				reg.getMaxLength(), messages, "username")) {
//			return resp.post(402);
//		}
//		if (!Validations.pattern(username, reg.getValidCharacter(), messages,
//				"username")) {
//			return resp.post(403);
//		}
//		if (!Validations.notEmpty(password, messages, "password")) {
//			return resp.post(404);
//		}
//		if (reg.getVerifyMode() == GlobalRegister.VERIFY_MODE_EMAIL
//				&& !Validations.notEmpty(email, messages, "email")) {
//			return resp.post(405);
//		}
//		if (!Validations.email(email, messages, "email")) {
//			return resp.post(406);
//		}
//		if (!Validations.pattern(gender, "[F,M]", messages, "gender")) {
//			return resp.post(407);
//		}
//		return null;
//	}
//
//	private String validateRegisterMessage(HttpServletRequest request,
//			Response resp, GlobalRegister reg, String username, String email,
//			Integer verifyMode) {
//		List<String> messages = resp.getMessages();
//		if (!Validations.notEmpty(username, messages, "username")) {
//			return resp.badRequest();
//		}
//		if (!Validations.notEmpty(email, messages, "email")) {
//			return resp.badRequest();
//		}
//		if (!Validations.notNull(verifyMode, messages, "verifyMode")) {
//			return resp.badRequest();
//		}
//		User registerUser = userService.findByUsername(username);
//		if (!Validations.exist(registerUser)) {
//			return resp.notFound();
//		}
//		if (!registerUser.getEmail().equals(email)) {
//			return resp.notFound("email not found: " + email);
//		}
//		if (reg.getMode() == GlobalRegister.MODE_OFF) {
//			return resp.warning("register.off");
//		}
//		return null;
//	}
//
//	private String validateForgotPasswordSubmit(HttpServletRequest request,
//			Response resp, String username, String email, String captcha) {
//		List<String> messages = resp.getMessages();
//		if (!Captchas.isValid(captchaService, request, captcha)) {
//			return resp.post(100, "error.captcha");
//		}
//		if (!Validations.notEmpty(username, messages, "username")) {
//			return resp.post(401);
//		}
//		if (!Validations.notEmpty(email, messages, "email")) {
//			return resp.post(402);
//		}
//
//		User forgotUser = userService.findByUsername(username);
//		if (!Validations.exist(forgotUser)) {
//			return resp.post(501, "forgotPassword.usernameNotExist",
//					new String[] { username });
//		}
//		if (!StringUtils.equals(forgotUser.getEmail(), email)) {
//			return resp.post(502, "forgotPassword.emailNotMatch");
//		}
//		return null;
//	}
//
//	@Autowired
//	private CaptchaService captchaService;
//	@Autowired
//	private MemberGroupService groupService;
//	@Autowired
//	private OrgService orgService;
//	@Autowired
//	private UserService userService;
//}
